package com.inspur.cloud.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StringUtil {
    public static String specialStr(String str) {
        String specialStr[] = {"\\", "_", "%", "[", "]", "^"};
        for (String string : specialStr) {
            str = str.replace(string, "\\" + string);
        }
        return str;
    }
    public static boolean isEmpty(String str){
        if(str==null) return true;
        if(str.equals("")) return true;
        return false;
    }

    public  static int getLevel(String path){
        if(isEmpty(path)){
            return 0;
        }
        return findStr(path,"/")-1;
    }

    public static String getParent(String path){
        if(isEmpty(path)){
            return "";
        }
        if(path.indexOf("/")<0){
            return "";
        }
        int lastDirectory=path.lastIndexOf("/");
        return lastDirectory==0?"/":path.substring(0,lastDirectory);
    }

    public static int findStr(String srcText, String keyword) {
        return findStr(srcText, keyword, 0);
    }

    public static int findStr(String srcText, String keyWord, int pos) {
        int i, j, k = 0;
        i = pos;
        j = 0;
        while (i < srcText.length() && j < keyWord.length()) {
            if (srcText.charAt(i) == keyWord.charAt(j)) {
                ++i;
                ++j;
                if (j == keyWord.length()) {
                    k = k + 1;// k++
                    j = 0;
                }
            } else {
                i = i - j + 1;
                j = 0;
            }
        }
        return k;
    }

    public static String concat(List<String> list, String concat){
        if(list==null||list.size()==0){
            return "";
        }
        String result="";
        for (String s : list) {
            result=result+s+concat;
        }
        result=result.substring(0,result.lastIndexOf(concat));
        return result;
    }

    public static String concatIntList(List<Integer> list, String concat) {
        if (list == null || list.size() == 0) {
            return "";
        }
        String result = "";
        for (Integer s : list) {
            result = result + s + concat;
        }
        result = result.substring(0, result.lastIndexOf(concat));
        return result;
    }

    public static String concatIntListDistinct(List<Integer> list, String concat) {
        if (list == null || list.size() == 0) {
            return "";
        }
        List<Integer> resultList = noRepeated(list);
        String result = "";
        for (Integer s : resultList) {
            result = result + s + concat;
        }
        result = result.substring(0, result.lastIndexOf(concat));
        return result;
    }

    public static boolean isLetterAndNumber(String str) {
        if (isEmpty(str)) return false;
        String regex = "^[a-zA-Z0-9]+$";
        return str.matches(regex);
    }

    public static List<Integer> noRepeated(List<Integer> list) {
        List<Integer> result = new ArrayList();
        list.stream().distinct().forEach(e -> result.add(e));
        return result;
    }

    public static List<String> splitHidden(String hidden){
        List<String> hiddenList=new ArrayList<String>();
        if(StringUtil.isEmpty(hidden)){
            return hiddenList;
        }
        hidden=hidden.replace("'","");
        String[] arr=hidden.split(",");
        if(arr.length>0){
            hiddenList= Arrays.asList(arr);
        }
        return hiddenList;
    }
}
